<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Http\Model\Goods;
use App\Http\Model\User;
use App\Http\Model\Menu;
use App\Http\Model\Banner;
use App\Http\Model\OrdersInfo;
use App\Http\Model\Address;
use App\Http\Model\Fenxiao;
use App\Http\Model\IntegralOrders;
use App\Http\Model\ClothingAttr;
use App\Http\Model\Company;
use App\Http\Model\Orders;
use App\Http\Model\System;
use DB;
use Config;

use EasyWeChat\Payment\Order;
use EasyWeChat\Foundation\Application;
use Intervention\Image\Facades\Image;
use EasyWeChat\Message\Text;

class IndexController extends Controller{
	// 商城首页
    public function index(Request $request){
        // session(['uid'=>'13']);
        // var_dump(session('uid'));die;
    	$menu = new Menu;
        $banner = new Banner;
        $video = new System;
        $menuInfo = $menu->get();
        $bannerInfo = $banner->orderBy("sort", "asc")->get();
        // dd($bannerInfo);
        $res = file_exists("upload/user/".session('uid').".jpg");
        if(!$res){
            $this->ewm();
        }
        $wechat = app('wechat');
        $js = $wechat->js;
        $domain_name = Config::get('app.domain_name');
        $videoStatus = $video->where('system_name','video')->first()->status;
        return view("home.index.index", ["menuInfo" => $menuInfo, "bannerInfo"=>$bannerInfo,"js"=>$js,"domain_name"=>$domain_name,'videoStatus' => $videoStatus]);

    }
    // 产品溯源
    public function suyuan($id){
        $menu = new Menu;
        $menuInfo = $menu->find($id);
        return view("home.index.suyuan", ["menuInfo" => $menuInfo]);
    }
    // 积分商城
    public function integral(){
    	$goods = new Goods;
        $fenxiao = new Fenxiao;
    	$user = new User;
        $userInfo = $user->where('id',session('uid'))->first();
    	$fenxiaoInfo = $fenxiao->select('integral_exchange_money')->first();
        $goodsInfo = $goods->where('exchange_status','1')->get();

    	return view("home.index.integral", [
    		"goodsInfo" => $goodsInfo,
            "fenxiaoInfo" => $fenxiaoInfo,
    		"userInfo" => $userInfo
    	]);
    }
    public static function volumn($id){
        $orders = new OrdersInfo;
        $sum = 0;
        $ordersInfo = $orders->where('goods_id',$id)->get();
        if(count($ordersInfo)){
            foreach ($ordersInfo as $key => $value) {
                $sum += $value->num;
            }
        }
        echo $sum;
    }

    // ajax获取商品
    public function getGoodsIntegralList(Request $request){
        $num = $request->input("num");
        $goods = new Goods;
        $goodsInfo = $goods->where("goods.exchange_status", "1")->skip($num)->take(8)->get();
        foreach ($goodsInfo as $k => $v) {
            $goodsInfo[$k]["sum"] = count($v->comment);
        }
        echo json_encode($goodsInfo);die;
    }

    public function  test(){
        // $config = [
        //   // ...
        // ];
        // $app = new Application($config);
        $app = app('wechat');
        $oauth = $app->oauth;
        // 获取 OAuth 授权结果用户信息
        $user = $oauth->user();
        dd($user);
        session(['wechat_user'=>$user->toArray()]);
        dd(session('wechat_user'));
        $targetUrl = empty(session('target_url')) ? '/' : session('target_url');
        return redirect($targetUrl);
        header('location:'. $targetUrl); // 跳转到 user/profile

    }
    public function detail($id){
        $goods = new Goods;
        $fenxiao = new Fenxiao;
        $user = new User;
        $address = new Address;
        $userInfo = $user->where('id',session('uid'))->first();
        $goodsInfo = $goods->find($id);
        $fenxiaoInfo = $fenxiao->first();
        $addressInfo = $address->where("user_id",session("uid"))->orderBy("type","desc")->get();
        $domain_name = Config::get('app.domain_name');
        $wechat = app('wechat');
        $js = $wechat->js;
        return view('home.index.detail',[
        	"goodsInfo" => $goodsInfo,
        	"fenxiaoInfo" => $fenxiaoInfo,
        	"userInfo" => $userInfo,
            "addressInfo" => $addressInfo,
            "js"=>$js,
            "domain_name"=>$domain_name
        ]);
    }
    public function pay(Request $request){
        // dd($request->all());
        $goods = new Goods;
        $user = new User;
        $fenxiao = new Fenxiao;
        $clothingAttr = new ClothingAttr;
        $integralOrders = new IntegralOrders;
        $fenxiaoInfo = $fenxiao->first();
        $userInfo = $user->select('address.id')->leftJoin('address','user_id','=','user.id')->where('user.id',session('uid'))->where('type',1)->first();
        $goodsInfo = $goods->find($request->input("goods"));
        $clothingAttrInfo = $clothingAttr->find($request->input("spec"));
        $integralOrders->orders_num = date('Ymd') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT);
        $integralOrders->num = $request->input("num");
        $integralOrders->goods_id = $goodsInfo->id;
        $integralOrders->goods_name = $goodsInfo->name;
        $integralOrders->goods_spec = $goodsInfo->spec;
        $integralOrders->pic = $goodsInfo->pic;
        $integralOrders->price = $clothingAttrInfo->price*$fenxiaoInfo->integral_exchange_money;
        $integralOrders->user_id = session("uid");
        $integralOrders->clothing_attr_id = $clothingAttrInfo->id;
        $integralOrders->spec = $clothingAttrInfo->clothingSpec->name;
        $integralOrders->address_id = $request->input("address");
        $integralOrders->status = 0;
        $integralOrders->save();
        $userInfo = $user->find(session("uid"));
        $userInfo->integral = $userInfo->integral - $clothingAttrInfo->price*$fenxiaoInfo->integral_exchange_money;
        $userInfo->save();
        echo 1;die;
    }
    //关于公司
    public function company(){
        $company = new Company;
        $companyInfo = $company->find(1);
    	return view('home.index.company',['companyInfo'=>$companyInfo]);
    }
    //线下门店
    public function store(){
        $company = new Company;
        $storeInfo = $company->find(2);
        return view('home.index.store',['storeInfo'=>$storeInfo]);
    }
    //客服新订单提醒
    public function orderTemplate(Request $request){
        $app = app('wechat');
        $user = new User;
        $userInfo = $user->where('id',session('uid'))->first();
        $kefuInfo = $user->where('user_type','1')->get();
        $notice = $app->notice;
        $template_id = 'kg3QU20n8gUugIidJok_9TOVhEKkyZX74udSmMuA76A';
        $url = Config::get('app.domain_name').'admin';
        $data = array(
            "first"=>'您收到了一条新的订单，请及时登录后台处理！',
            "keyword1"=>$request->input('num'),
            "keyword2"=>'天路本草！',
            "keyword3"=>$request->input('sum'),
            "keyword4"=>'微信支付',
            "keyword5"=>$userInfo->username,
            "remark"=>'请及时处理！',
        );
        foreach ($kefuInfo as $key => $value) {
          $res = $notice->uses($template_id)->withUrl($url)->andData($data)->andReceiver($value->open_id)->send();
        }
        echo 1;
     }
}
